﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace VISUALFINDR
{
    public partial class App : Application
    {
        public App()
        {
            UnhandledException += new EventHandler<ApplicationUnhandledExceptionEventArgs>(Application_UnhandledException);

            InitializeComponent();

            uploadController = new UploadController();
            pollingController = new PollingController();

            // Phone-specific initialization
            InitializePhoneApplication();            
        }

        // Easy access to the root frame
        public PhoneApplicationPage MainPage { get; private set; }
        public PhoneApplicationFrame RootFrame { get; private set; }

        //Global variables for the WriteableBitmap objects used throughout the application.
        public static WriteableBitmap CapturedImage;
        public static String Text;
        //public static WriteableBitmap CroppedImage;
        public static UploadController uploadController;
        public static PollingController pollingController;

        // Code to execute on Unhandled Exceptions
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred, break in the debugger
                System.Diagnostics.Debugger.Break();
            }
            else
            {
                // By default show the error
                e.Handled = true;
                MessageBox.Show(e.ExceptionObject.Message + Environment.NewLine + e.ExceptionObject.StackTrace,
                    "Error", MessageBoxButton.OK);
            }
        }

        private void Application_Launching(object sender, Microsoft.Phone.Shell.LaunchingEventArgs e)
        {
           
        }

        private void Application_Closing(object sender, Microsoft.Phone.Shell.ClosingEventArgs e)
        {
            
        }

        private void Application_Activated(object sender, Microsoft.Phone.Shell.ActivatedEventArgs e)
        {

        }

        private void Application_Deactivated(object sender, Microsoft.Phone.Shell.DeactivatedEventArgs e)
        {

        }

        #region Phone application initialization

        // Avoid double-initialization
        private bool phoneApplicationInitialized = false;

        // Do not add any additional code to this method
        private void InitializePhoneApplication()
        {
            if (phoneApplicationInitialized)
                return;

            // Create the frame but don't set it as RootVisual yet; this allows the splash
            // screen to remain active until the application is ready to render.
            RootFrame = new PhoneApplicationFrame();

            RootFrame.Navigated += CompleteInitializePhoneApplication;

            // Handle navigation failures
            //RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            Text = null;

            // Ensure we don't initialize again
            phoneApplicationInitialized = true;
        }

        // Do not add any additional code to this method
        private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
            // Set the root visual to allow the application to render
            if (RootVisual != RootFrame)
                RootVisual = RootFrame;

            // Remove this handler since it is no longer needed
            RootFrame.Navigated -= CompleteInitializePhoneApplication;
        }

        #endregion

        internal static void UpdateText(String p)
        {
            Text = p;
        }

        internal static String GetText()
        {
            String txt = Text;
            Text = null;
            return txt;
        }

        public static void Reset()
        {
            App.CapturedImage = null;
            HistoryController.ClearHistory();
            pollingController.ForceReset();
            uploadController.Reset();
        }
    }
}